package engine;

/**
 * Used for simple time storage of hours, minutes and seconds passed from 00:00:00.
 * @author Johnny
 */
public class Time implements Comparable<Time> {

    private int totalSeconds;

    public Time(int seconds) {
        // Making it idiotproof
        if (seconds >= 0) {
            this.totalSeconds = seconds;
        } else {
            this.totalSeconds = 0;
        }
    }

    public Time(int hours, int minutes, int seconds) {
        // Idiotproofing
        if (hours < 0) {
            hours = 0;
        } else if (hours > 23) {
            hours = 23;
        }

        if (minutes < 0) {
            minutes = 0;
        } else if (minutes > 59) {
            minutes = 59;
        }

        if (seconds < 0) {
            seconds = 0;
        } else if (seconds > 59) {
            seconds = 59;
        }
        // End of idiotproofing :)

        totalSeconds = seconds + 60 * (minutes + 60 * (hours));
    }

    /**
     * Returns the total ammount of seconds.
     * @return the total ammount of seconds.
     */
    public int getTotalSeconds() {
        return totalSeconds;
    }

    /**
     * Sets the total ammount of seconds.
     * @param totalSeconds the total ammount of seconds.
     */
    public void setTotalSeconds(int totalSeconds) {
        this.totalSeconds = totalSeconds;
    }

    /**
     * Returns the number of seconds.
     * @return the number of seconds.
     */
    public int getSeconds() {
        return totalSeconds % 60;
    }

    /**
     * Returns the number of mintues.
     * @return the number of mintues.
     */
    public int getMinutes() {
        return (totalSeconds % 3600) / 60;
    }

    /**
     * Returns the number of hours.
     * @return the number of hours.
     */
    public int getHours() {
        return totalSeconds / 3600;
    }

    /**
     * Sets the number of seconds.
     * @param seconds The number of seconds.
     */
    public void setSeconds(int seconds) {
        if (seconds < 0) {
            seconds = 0;
        } else if (seconds > 59) {
            seconds = 59;
        }

        totalSeconds -= getSeconds();
        totalSeconds += seconds;
    }

    /**
     * Sets the number of minutes.
     * @param minutes The number of minutes.
     */
    public void setMinutes(int minutes) {
        if (minutes < 0) {
            minutes = 0;
        } else if (minutes > 59) {
            minutes = 59;
        }

        totalSeconds -= getMinutes() * 60;
        totalSeconds += minutes * 60;
    }

    /**
     * Sets the number of hours.
     * @param hours The number of hours.
     */
    public void setHours(int hours) {
        if (hours < 0) {
            hours = 0;
        } else if (hours > 59) {
            hours = 59;
        }

        totalSeconds -= getHours() * 3600;
        totalSeconds += hours * 3600;
    }

    /**
     * Sets the time.
     * @param hours the number of hours.
     * @param minutes the number of minutes.
     * @param seconds the number of seconds.
     */
    public void setTime(int hours, int minutes, int seconds) {
        setHours(hours);
        setMinutes(minutes);
        setSeconds(seconds);
    }

    /**
     * Returns the time interval between the two in seconds.
     * @param t The time that will be substracted from this object
     * @return The interval between the two in seconds.
     */
    public Time substract(Time t) {
        return new Time(totalSeconds - t.getTotalSeconds());
    }

    @Override
    public String toString() {
        int hours = getHours();
        int minutes = getMinutes();
        int seconds = getSeconds();
        StringBuffer sb = new StringBuffer(8);
        if (hours < 10) {
            sb.append(0);
        }
        sb.append(hours);
        sb.append(':');
        if (minutes < 10) {
            sb.append(0);
        }
        sb.append(minutes);
        sb.append(':');
        if (seconds < 10) {
            sb.append(0);
        }
        sb.append(seconds);

        return sb.toString();
    }

    public int compareTo(Time o) {
        return totalSeconds - o.getTotalSeconds();
    }
}
